package Model;


import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author thuydao
 */
public class M_Contact {
    private String malh;
    private String diachi;
    private String sdt;
    private String email;
    private String ngaytl;
         
    private Connection conn;
    public void connect(){
         try {
            String userName = "root"; // root là mặc định của MySQL.
            String passWord = "";
            String url = 
            "jdbc:mysql://localhost:3306/handmade?useUnicode=true&characterEncoding=UTF-8"; 
           
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, passWord);
            }
            catch (Exception e) {
                System.out.println("Diver sai");
            }
        
    }
    
    public M_Contact(){}
    public M_Contact(String strmalh, String strdiachi, String strsdt, String strngaytl, String stremail){
        this.malh = strmalh;
        this.diachi = strdiachi;
        this.sdt = strsdt;
        this.ngaytl = strngaytl;
        this.email = stremail;
    }

    /**
     * @return the malh
     */
    public String getMalh() {
        return malh;
    }

    /**
     * @param malh the malh to set
     */
    public void setMalh(String malh) {
        this.malh = malh;
    }

    /**
     * @return the diachi
     */
    public String getDiachi() {
        return diachi;
    }

    /**
     * @param diachi the diachi to set
     */
    public void setDiachi(String diachi) {
        this.diachi = diachi;
    }

    /**
     * @return the sdt
     */
    public String getSdt() {
        return sdt;
    }

    /**
     * @param sdt the sdt to set
     */
    public void setSdt(String sdt) {
        this.sdt = sdt;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the ngaytl
     */
    public String getNgaytl() {
        return ngaytl;
    }

    /**
     * @param ngaytl the ngaytl to set
     */
    public void setNgaytl(String ngaytl) {
        this.ngaytl = ngaytl;
    }
    
    /*public boolean insertContact(String strdiachi, String strsdt, String stremail, String strngaytl){
            connect();
         
          try{
                
            
            String sql_insert_contact = "insert into lienhe(DiaChi, Sdt, Email, NgayTL) values("; 
            sql_insert_contact += "'"+strdiachi+"',"; 
            sql_insert_contact += "'"+strsdt+"',"; 
            sql_insert_contact += "'"+stremail+"',";                        
            sql_insert_contact += "'"+strngaytl+"')";
         
            java.sql.Statement st = conn.createStatement();
           int val = st.executeUpdate(sql_insert_contact);
            //result = "Đã thêm "+String.valueOf(val)+" dòng thành công";
             // System.out.println("Đã thêm "+String.valueOf(val)+" dòng thành công");
           if(val>0)
               return true;
            }
            catch (SQLException s){
                System.out.println("SQL statement is not executed! <br>"+s.getMessage());
            //result = "SQL statement is not executed! <br>"+s.getMessage();
            }
       return false;
          
   }*/
    
      public boolean updateContact(){
            connect();
            
            
           
          try{
            String sql_update_Contact = "update lienhe set DiaChi=?, SDT=?, Email=?, NgayTL=? where MaLH=?"; 
            PreparedStatement stm = conn.prepareStatement(sql_update_Contact);
            stm.setString(1, diachi);
            stm.setString(2, sdt);
            stm.setString(3, email);
            stm.setString(4, ngaytl);
            
            stm.setString(5, malh);
            
           
            int val= stm.executeUpdate();
            stm.close();
            conn.close();
           if(val>0)
                return true;
        
            }
            catch (SQLException s){
                System.out.println("QL insert TAIKHOAN statement is not executed!"+ s.getMessage());
            }
          return false;
   }
      
      public M_Contact[] show_Contact(){
            connect();
            ResultSet rs=null;
            M_Contact[] result=null;
            try{
            String sql_show_Contact = "select * from lienhe";
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_show_Contact);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                String malh = rs.getString("MaLH");
                String diachi = rs.getString("DiaChi");
                String sdt = rs.getString("SDT");
                String ngaytl = rs.getString("NgayTL");
                String email = rs.getString("Email");
                
                M_Contact con = new M_Contact(malh, diachi, sdt, ngaytl, email);
                list.add(con);
                
                
            }
            result = new M_Contact[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return result;
            
    }
}
